package controllers

import (
	"fmt"
	"go/dao"
	"go/models"
	"go/pkg/app"
	"go/pkg/e"
	"go/services"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func Addstudent(c *gin.Context) {
	m := make(map[string]string, 1024)
	c.ShouldBind(&m)
	fmt.Println(m)
	id := m["id"]
	name := m["name"]
	sex := m["sex"]
	birth := m["birth"]
	mate := m["mate"]
	honor := m["honor"]
	child := m["child"]
	father := m["father"]
	mother := m["mother"]

	tmpId, err := strconv.Atoi(id)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	// tmpSage, err := strconv.Atoi(sage)
	// if err != nil {
	// 	app.Error(c, e.ERROR, err, err.Error())
	// 	return
	// }

	err = services.Addstudent(models.Family{
		Id:     id,
		Name:   name,
		Sex:    sex,
		Birth:  birth,
		Mate:   mate,
		Honor:  honor,
		Child:  child,
		Father: father,
		Mother: mother,
	})

	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}

	u := models.Family{
		Id:     id,
		Name:   name,
		Sex:    sex,
		Birth:  birth,
		Mate:   mate,
		Honor:  honor,
		Child:  child,
		Father: father,
		Mother: mother,
	}
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, u, "添加成功")
}

func Delstudent(c *gin.Context) {
	json := make(map[string]string, 1024)
	c.ShouldBind(&json)
	fmt.Println(json)
	id := json["id"]
	db := dao.GetDB()
	err := db.Table("student_message").Where("id = ?", id).Update("isdel", true).Error
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "删除成功")
}

func Updatestudent(c *gin.Context) {
	m := make(map[string]string, 1024)
	c.ShouldBind(&m)
	fmt.Println(m)
	id := m["id"]
	name := m["name"]
	sex := m["sex"]
	birth := m["birth"]
	mate := m["mate"]
	honor := m["honor"]
	child := m["child"]
	father := m["father"]
	mother := m["mother"]

	tmpId, err := strconv.Atoi(id)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	tmpSage, err := strconv.Atoi(sage)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}

	err = services.Updatestudent(models.Family{
		Name:   name,
		Sex:    sex,
		Birth:  birth,
		Mate:   mate,
		Honor:  honor,
		Child:  child,
		Father: father,
		Mother: mother,
	})

	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}

	u := models.Family{
		Name:   name,
		Sex:    sex,
		Birth:  birth,
		Mate:   mate,
		Honor:  honor,
		Child:  child,
		Father: father,
		Mother: mother,
	}
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, u, "更新成功")
}

func Getstudent(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	searchName := ""
	if arg := c.Query("searchName"); arg != "" {
		searchName = arg
	}
	studentParam := map[string]interface{}{
		"page":       page,
		"limit":      limit,
		"searchName": searchName,
	}
	info, total, err := services.Getstudent(studentParam)
	//fmt.Println(info);
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "OK")
}
